//商品关注丶浏览记录丶我的收藏
'use strict';
import React, {Component} from 'react';
import styles from '../style/styles';
import Header from '../common/header';
import Icon from 'react-native-vector-icons/FontAwesome';
import Util from '../common/util';
import Toast from '../common/ToastUtil'
import * as URLconstant from '../constant/URLconstant'
import GoodsIndex from '../goods/goodsIndex'
import {
    View,
    Text,
    ScrollView,
    TouchableOpacity,
    Image,
    ListView,
    ActivityIndicatorIOS,
    ProgressBarAndroid,
    Platform,
    FlatList
} from 'react-native';

export default class userCollection extends Component {
    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.successGet = this.successGet.bind(this)
        this.errorGet = this.errorGet.bind(this)
        //this.goodsList=this.goodsList.bind(this)
        this.renderFooter = this.renderFooter.bind(this)
        this.onEndReached = this.onEndReached.bind(this)
        this.successDelete = this.successDelete.bind(this)
        this.deleteCollection = this.deleteCollection.bind(this)
        this.goodsList = this.goodsList.bind(this)
        this.state = {
            user_id: '',
            page: 1,
            goods_list: null,
            isOver: false,
            loaded: false
        };
    }


    async componentDidMount() {
        let params = this.props.route.params
        await this.setState({
            user_id: params.user_id
        })
        this.getCollection()
    }

    getCollection() {
        let user_id = this.state.user_id;
        let page = this.state.page
        let datakey = [];
        datakey['page'] = page;
        datakey['user_id'] = user_id;
        let url = URLconstant.FAVORITESLIST_URL
        Util.get(url, datakey, this.successGet, this.errorGet)
    }

    successGet(datas) {
        //LOG(JSON.stringify(datas))
        if (datas.status == 0) {
            let data = datas.data;
            let total = data.total;
            let list = data.list;
            let getpage = total.page;
            let isOver = this.state.isOver;
            let page = this.state.page;
            if (getpage == page) {
                isOver = true;
            }
            if (list == '') {
                isOver = true;
            }
            let newDatas = list;
            let goods_list = this.state.goods_list;
            if (goods_list == null) {
                goods_list = []
            }
            Array.prototype.push.apply(goods_list, newDatas);
            this.setState({
                total: total,
                goods_list: goods_list,
                page: page + 1,
                isOver: isOver,
                loaded: true,

            });

        } else {
            this.setState({
                isOver: true,
                loaded: true,
                goods_list: []
            })
        }

    }

    errorGet(err) {
        LOG(err)
    }

    //listView脚部
    renderFooter() {
        let that = this;
        if (that.state.isOver) {
            return (<View style={{alignItems: "center", width: Util.size.width, justifyContent: 'center', margin: 10,}}>
                    <Text>亲,只有这么多了哦...</Text>
                </View>
            );
        } else {
            if (Platform.OS === 'android') {
                return (
                    <View style={{alignItems: "center", justifyContent: 'center', width: Util.size.width, height: 20}}>
                        <Text>加载中...</Text>
                    </View>
                );
            }
        }

    }

    //listView上拉触发
    onEndReached() {
        let that = this;
        if (!that.state.isOver) {
            that.getCollection();
        } else {
            //Toast.show('亲,只有这么多了哦...');
        }
    }

    //删除收藏
    deleteCollection(favorites_id) {
        let url = URLconstant.DELETEFAVORITES_URL
        let user_id = this.state.user_id;
        let datakey = [];
        datakey['favorites_id'] = favorites_id;
        datakey['user_id'] = user_id;
        Util.get(url, datakey, this.successDelete, this.errorGet)

    }

    successDelete(datas) {
        //LOG(datas)
        if (datas.status == 0) {
            Toast.show(datas.msg)
            this.setState({
                dataSource: [],
                page: 1
            })
            this.getCollection()
        }
    }

    goGoodsIndex(goods_id) {
        this.props.navigator.push(
            {
                component: GoodsIndex,
                passProps: {
                    goods_id: goods_id,

                }
            }
        );
    }


    goodsList=({item})=> {
        return (
            <View style={styles.collection_list}>
                <TouchableOpacity onPress={() => this.goGoodsIndex(item.goods_id)}>
                    <Image
                        resizeMode="stretch"
                        style={styles.collection_img}
                        source={{uri: item.default_image}}/>
                </TouchableOpacity>
                <View style={styles.collection_right}>
                    <Text style={styles.collection_title}>{item.goods_name}</Text>
                    <Text style={styles.collection_price}>￥{item.price}</Text>
                    <TouchableOpacity style={styles.collection_delete}
                                      onPress={() => this.deleteCollection(item.favorites_id)}>
                        <Icon name={'trash-o'} size={20} color={'#333'}/>
                    </TouchableOpacity>
                </View>
            </View>
        )

    }

    render() {
        return (
            <View style={{flex: 1, backgroundColor: '#f2f2f2'}}>
                <Header navigation={this.props.navigation} title={'我的收藏'}/>
                {!this.state.loaded ?
                    <View style={{flex: 1}}>{Util.loading()}</View>
                    :
                    <View style={{flex: 1}}>
                        {
                            this.state.goods_list.length == 0 ?
                                <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
                                    <Text>没有收藏</Text>
                                </View> :
                                <FlatList
                                    style={{paddingTop: 10}}
                                    data={this.state.goods_list}
                                    renderItem={this.goodsList}
                                    onEndReached={this.onEndReached.bind(this)}
                                    keyExtractor={item => item.goods_id}
                                />
                        }

                    </View>
                }

            </View>
        )
    }
}